﻿using JustSiteWorker;
using System;
using System.Security.Cryptography;
using System.Text;

public class UserViewModel : User
{
    public bool ValidateError { get; set; }

    public UserViewModel()
    {
        ValidateError = false;
    }

    public User ConvertToUser()
    {
        User user = new User()
        {
            Name = Name,
            Email = Email,
            Password = LoginHelper.EncodePassword(Password, Name),
            Avatar = Avatar,
            Role = 1,
            UrlId = UrlId
        };

        return user;
    }
}

public class LoginHelper
{
    /// <summary>
    /// encode password to md5
    /// </summary>
    /// <param name="originalPassword"></param>
    /// <returns>md5 password </returns>
    public static string EncodePassword(string originalPassword, string key)
    {
        if (key != null)
            key = key.ToLower();

        originalPassword = "JustSite" + originalPassword + key;
        //Declarations
        Byte[] originalBytes;
        Byte[] encodedBytes;
        MD5 md5 = new MD5CryptoServiceProvider();
        originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
        encodedBytes = md5.ComputeHash(originalBytes);

        //Convert encoded bytes back to a 'readable' string
        return BitConverter.ToString(encodedBytes);
    }
}